home *** CD-ROM | disk | FTP | other *** search
- /* ====================================================================
- * ISREADY.C - program to enhance batch files by checking to see if the
- * requested drive letter is ready - usable - without attempting to read
- * the device. This offers the advantage of being able to prompt the
- * user without having DOS "ABORT, RETRY, FAIL" prompt given instead.
- * A zero errorlevel is returned if the drive is ready. Otherwise, a
- * non-zero code is returned.
- * =============================================================
- * Copyright (C) 1991 Bruce E. Högman. All Rights Reserved.
- * This program is dedicated to the public domain by the author.
- * =============================================================
- * RETURN CODES (DOS ERRORLEVEL IN .BAT FILES)
- *
- * The following values are "OR'd" into the return code:
- * 0: OK, is ready and readable.
- * 4: not ready
- * 8: removable device (may be reported with 0 or 4 codes)
- * 16: bad arguments (none or wrong length or wrong value)
- * Example: 8: is removable and ready.
- * 12: is removable and not ready.
- * 4: is not removable and not ready.
- * 16: bad arguments.
- * ====================================================================
- */
- #include <stddef.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <ctype.h>
- #include <dos.h>
- #include <io.h>
- #include "getdpb.h"
- DPB_DATA far *getdpb( void );
- int getdrive ( void ); /* returns drive: 0=a,25=z */
- int seldisk ( char dskltr ); /* returns int: 0=OK, -1=error */
- int seldiskn( int dsknbr ); /* returns int: 0=OK, -1=error */
- #define DEBUG 0
- #define NOT_READY 4
- #define BADARGS 16
- #define OK 0
- #define FALSE 0
- #define TRUE ~FALSE
- #define IGNORE 0
- #define RETRY 1
- #define ABORT 2
- static char msg[80];
- static int drive_ready=TRUE;
- static char drive_ltr;
- int movable (int drv);
- #pragma warn -par
- int far hndlherx( int errval, int ax, int bp, int si)
- { static char *err_msg[] = {
- "write protect", "unknown unit", "drive not ready", "unknown command",
- "data error (CRC)", "bad request", "seek error", "unknown media type",
- "sector not found", "printer out of paper", "write fault", "read fault",
- "general failure", "reserved message", "reserved message",
- "invalid disk change" };
- unsigned di; int drive; int errorno;
- di = _DI; drive_ready = FALSE;
- if (ax < 0)
- { sprintf(msg,"Error: device error on drive %c:\n",drive_ltr);
- }
- else
- { drive = ax & 0x00FF;
- errorno = di & 0x00FF;
- sprintf(msg,"Error: %s on drive %c:\n",
- err_msg[errorno], 'A' + drive);
- }
- hardresume(IGNORE);
- return IGNORE;
- }
- int far hndlherx( int errval, int ax, int bp, int si);
- int main (int argc, char *argv[])
- { int savedrive, rcseldisk, verbose=0, rc=0; DPB_DATA *dptr; int rcmovable;
- int drvn; char svdrv;
- if (argc < 2)
- { printf("Syntax: isready drive_letter [/v] \n");
- printf("/v: verbose: display messages \n");
- rc = BADARGS; goto pgm_exit;
- }
- if (argc > 2)
- { if (stricmp(argv[2],"/v") == 0) verbose=TRUE;}
- savedrive = getdrive(); /* Remember current CWD drive */
- #if DEBUG
- svdrv = (char) (savedrive + (int) 'A');
- printf("Current drive was %c, %d\n",svdrv,savedrive);
- #endif
- rcseldisk = seldisk(argv[1][0]); /* Select new current drive */
- if (rcseldisk < 0)
- { rc = BADARGS; goto pgm_exit;}
- drive_ltr = toupper(argv[1][0]); drvn = drive_ltr - 'A' + 1;
- harderr(hndlherx); /* Define interrupt 24 handler */
- dptr = getdpb(); /* Read drive parm block */
- rcmovable = movable(drvn)==0? 8: 0;
- if (drive_ready == FALSE)
- { rc = NOT_READY;
- if (verbose)
- { if (rcmovable) { printf("Removable ");} else { printf("Fixed ");}
- printf("drive %c is not ready\n",drive_ltr); printf("%s",msg);
- }
- }
- else
- { if (dptr->DPBV == '\xff') /* invalid DPB */
- { rc = NOT_READY;
- if (verbose) { printf("invalid DPB found\n"); }
- }
- else
- { if (dptr->DPBV == '\x00')
- { if (verbose)
- { printf("valid DPB found\n");
- printf("DPB drive: %xc\n",dptr->drv);
- printf("DPB media: %xc\n",dptr->mdb);
- }
- }
- else
- { if (verbose) { printf("unknown DPB found\n");}
- }
- }
- }
- pgm_exit:
- if (rc != BADARGS) { rc |= rcmovable; }
- rcseldisk = seldiskn(savedrive);
- return rc;
- }
-